package com.ext.portlet.campussearch.action;

import java.util.List;

import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.ext.portlet.common.MBConstants;
import com.ext.portlet.registration.model.CollStudent;
import com.ext.portlet.registration.model.College;
import com.ext.portlet.registration.model.Staff;
import com.ext.portlet.registration.service.CollStudentLocalServiceUtil;
import com.ext.portlet.registration.service.CollegeLocalServiceUtil;
import com.ext.portlet.registration.service.StaffLocalServiceUtil;

import com.liferay.portal.dao.orm.hibernate.DynamicQueryImpl;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.struts.PortletAction;

public class CampusSearchAction extends PortletAction {

	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		
		String cmd = ParamUtil.getString(req, Constants.CMD);
		System.out.println("cmd>>>>>>>"+cmd);
		int menubar = ParamUtil.getInteger(req, "search");
		System.out.println("menubar>>>>>"+menubar);
		
		if(cmd.equals("search_menu")){
			
			switch(menubar){
			
			case 1 :{
						req.setAttribute("options", "College");
						break;
					}
			case 2 :{
						req.setAttribute("options", "University");
						break;
					}
			case 3 :{
						req.setAttribute("options", "Student");
						break;
					}
			case 4 :{
						req.setAttribute("options", "Staff");
						break;
					}
		    default:{
					req.setAttribute("options", "College");
					}
			}
					
		}else if(cmd.equals("search")){
			
			String hidd_val = ParamUtil.getString(req, "hidd_val");
			String name = ParamUtil.getString(req, "name");
			String countryId = ParamUtil.getString(req, "countryId");
			String regionId = ParamUtil.getString(req, "regionId");			
			String cityId = ParamUtil.getString(req, "cityId");
			
			if(hidd_val.equals("College")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(College.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("name", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<College> colleges = (List)CollegeLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] groupIds = new long[colleges.size()];
                
                for(int i=0; i<colleges.size(); i++){
                	College college = colleges.get(i);
                	groupIds[i]= college.getGroupId();	                	
                }     
				
				System.out.println("College List>>>>>>"+groupIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", groupIds);
				
			} else if(hidd_val.equals("Student")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(CollStudent.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("firstName", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<CollStudent> students = (List)CollStudentLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] studentIds = new long[students.size()];
                
                for(int i=0; i<students.size(); i++){
                	CollStudent student = students.get(i);
                	studentIds[i]= student.getCollStudentId();	                	
                }     
				
				
				System.out.println("Student List>>>>>>"+studentIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", studentIds);
				
			} else if(hidd_val.equals("Staff")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(Staff.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("firstName", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<Staff> staffs = (List)StaffLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] staffIds = new long[staffs.size()];
                
                for(int i=0; i<staffs.size(); i++){
                	Staff staff = staffs.get(i);
                	staffIds[i]= staff.getStaffId();	                	
                }     
				
				
				System.out.println("Staff List>>>>>>"+staffIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", staffIds);
				
			} 
			
			req.setAttribute("PAGE", "/html/portlet/ext/campussearch/search_result.jsp");
			return mapping.findForward("portlet.ext.campussearch.view");
			
		}
			
		req.setAttribute("PAGE", "/html/portlet/ext/campussearch/search_result.jsp");
		return mapping.findForward("portlet.ext.campussearch.view");
	
	}

}
